﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RolePermission.aspx.cs" Inherits="DBA.Web.SystemManage.Permissions.RolePermission" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <%--<link href="/css/Styleunify.css" rel="stylesheet" type="text/css" />--%>
     <link href="../../Scripts/ZTree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" type="text/css" />
    <script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.bgiframe.js" type="text/javascript"></script>
    <script src="../../Scripts/lhgdialog/lhgdialog.js" type="text/javascript"></script>
    <script src="../../Scripts/Public.js" type="text/javascript"></script>
    <script src="../../Scripts/ZTree/js/jquery.ztree.all-3.2.min.js" type="text/javascript"></script>
    <script src="../../Scripts/ZTree/js/jquery.ztree.core-3.2.min.js" type="text/javascript"></script>
    <script src="../../Scripts/ZTree/js/jquery.ztree.excheck-3.2.min.js" type="text/javascript"></script>
    <script src="../../Scripts/ZTree/js/jquery.ztree.exedit-3.2.min.js" type="text/javascript"></script>
    
    <script type="text/javascript" language="javascript">
        function getChecked() {
            //获取节点ID
            alert($("#hidFunIDList").val());
        }


        //切换子系统权限下拉框的时候触发，加载子系统的权限树
        function ddlChange() {
            $.fn.zTree.init($("#treeDept"), setting, null);
        }


        //tree
        var setting = {
            async: {
                enable: true,
                url: getUrl
            },
            check: {
                enable: true
            },
            data: {
                key:{
			        title: "title"
		        },
                simpleData: {
                    enable: true

                }
            },
            view: {
                expandSpeed: "",
                showTitle: true
            },
            callback: {
                onAsyncSuccess: onAsyncSuccess,
                onAsyncError: onAsyncError,
                onCheck: zTreeOnCheck


            }
        };

        var setting1 = {
            check: {
                enable: false
            },
            data: {
                simpleData: {
                    enable: true
                }
            },

            view: {
                expandSpeed: ""
            },
            callback: {

        }
    };

    var log, className = "dark",
		startTime = 0, endTime = 0, perCount = 100, perTime = 100;
    function getUrl(treeId, treeNode) {
        return 'RoleTree.ashx?roleID=' + $("#hidRoleID").val() + '&';
    }
    function beforeExpand(treeId, treeNode) {
        if (!treeNode.isAjaxing) {
            startTime = new Date();
            treeNode.times = 1;
            ajaxGetNodes(treeNode, "refresh");
            return true;
        } else {
            alert("zTree 正在下载数据中，请稍后展开节点。。。");
            return false;
        }
    }
    function onAsyncSuccess(event, treeId, treeNode, msg) {
        var zTree = $.fn.zTree.getZTreeObj("treeDept");
        zTree.expandAll(true);
        var idList = $("#hidFunIDList").val().substring(1).split(',');
        for (var i = 0; i < idList.length; i++) {
            var node = zTree.getNodeByParam("id", idList[i], null);
            if (node != null) {
                if (node.children == undefined) {
                    zTree.checkNode(node, true, true);
                }

            }
        }
    }
    function onAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
        var zTree = $.fn.zTree.getZTreeObj("treeDept");
        alert("异步获取数据出现异常。");
        treeNode.icon = "";
        zTree.updateNode(treeNode);
    }
    function ajaxGetNodes(treeNode, reloadType) {
        var zTree = $.fn.zTree.getZTreeObj("treeDept");
        if (reloadType == "refresh") {
            treeNode.icon = "../../Scripts/ZTree/css/zTreeStyle/img/loading.gif";
            zTree.updateNode(treeNode);
        }
        zTree.reAsyncChildNodes(treeNode, reloadType, true);
    }

    function zTreeOnCheck(event, treeId, treeNode) {
        var zTree = $.fn.zTree.getZTreeObj("treeDept");
        var zUserTree = $.fn.zTree.getZTreeObj("treeUser");
        if (treeNode.checked) {
            var nodes = zTree.getCheckedNodes(true);
            var parentNode = zUserTree.getNodeByParam("id", '1', null);
            for (var i = 0; i < nodes.length; i++) {
                if (nodes[i].check_Child_State > -1 && nodes[i].level > 0) {
                    var userNode = zUserTree.getNodeByParam("id", nodes[i].id, null);
                    if (userNode == null) {//判断是否有此项权限,没有权限则加入      
                        var parentNodeList = zUserTree.getNodeByParam("id", nodes[i].pId, null);
                        zUserTree.addNodes(
                        parentNodeList
                        , { id: nodes[i].id, name: nodes[i].name, pid: nodes[i].pId });
                        $("#hidFunIDList").val($("#hidFunIDList").val() + "," + nodes[i].id);
                        continue;
                    }
                }
                if (nodes[i].check_Child_State == -1) {
                    var userNode = zUserTree.getNodeByParam("id", nodes[i].id, null);
                    if (userNode == null) {//判断是否有此项权限,没有权限则加入            
                        var parentNodeList = zUserTree.getNodeByParam("id", nodes[i].pId, null);
                        zUserTree.addNodes(
                        parentNodeList
                        , { id: nodes[i].id, name: nodes[i].name, pid: nodes[i].pId });
                        $("#hidFunIDList").val($("#hidFunIDList").val() + "," + nodes[i].id);
                    }
                }
            }
        }
        else {
            var nodes1 = zTree.getCheckedNodes(false);
            var nodeTmp = [];
            //记录节点的最大值
            var nodeMax = 0;
            for (var i = 0; i < nodes1.length; i++) {
                if (nodes1[i].check_Child_State >= -1 && nodes1[i].level > 0 && nodes1[i].isParent == false) {
                    var userNode = zUserTree.getNodeByParam("id", nodes1[i].id, null);
                    if (userNode != null) {
                        if (nodes1[i].level > nodeMax) {
                            nodeMax = nodes1[i].level;
                        }
                        zUserTree.removeNode(userNode);
                        $("#hidFunIDList").val($("#hidFunIDList").val().replace("," + nodes1[i].id, ""));
                    }
                }
                else {
                    if (nodes1[i].level > 0) {
                        nodeTmp.push(nodes1[i]);
                    }
                }
            }
            for (var j = nodeMax - 1; j >= 1; j--) {
                for (var m in nodeTmp) {
                    if (nodeTmp[m].level == j) {
                        var userNode = zUserTree.getNodeByParam("id", nodeTmp[m].id, null);
                        if (userNode != null) {
                            zUserTree.removeNode(userNode);
                            $("#hidFunIDList").val($("#hidFunIDList").val().replace("," + nodeTmp[m].id, ""));
                        }
                    }
                }
            }
            if (treeNode.level == 0) {
                $("#hidFunIDList").val("");
            }

        }
    };
    function dgDeleteNode() {
    }
    $(document).ready(function () {
        var zNodes = $.parseJSON($("#hidJsonTree").val());
        $.fn.zTree.init($("#treeDept"), setting, null);
        $.fn.zTree.init($("#treeUser"), setting1, zNodes);
        var zTree = $.fn.zTree.getZTreeObj("treeUser");
        zTree.expandAll(true);
    });
    var DG = frameElement.lhgDG;
    $(function () {
        DG.addBtn('obtn', '提 交', function () {
            if (confirm("是否确认保存权限?")) {
                __doPostBack("lnkSave", "");
            }
        });
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function (sender, e) {
            if ($("#hdResult").val() == "1") {
                alert("操作成功!");
                Public.CloseWindow();
            } else {
                if ($("#hdResult").val() == "-1") {
                    alert("不能修改【超级管理员】的权限!");
                } else {
                    alert("操作失败,请重试!");
                }
            }
        });
    });
    </script>
</head>
<body>
    <form id="form1" runat="server">
         <asp:ScriptManager ID="ScriptManager1" runat="server">
         </asp:ScriptManager>
    <table width="600px" >   
    <tr> <td align="left"><asp:HiddenField ID='hidFlag' runat="server" Value="0" />
    <asp:HiddenField ID='hidRoleID' runat="server" Value="0" />
    <asp:HiddenField ID='hidFunIDList' runat="server" Value="" />
     <asp:HiddenField ID='hidJsonTree' runat="server" Value="" />
   </td></tr>
    <tr>
    <td style="width:300px">   

           
    <div style="width:100%;height:490px;overflow:auto;margin-left:4px;text-align:left">
    <ul id="treeDept" class="ztree" style="margin-top:0; width:90%; height: 480px;"></ul>
    </div>    
    </td>       
     <td style="width:300px">
     <div style="width:100%;height:490px;overflow:auto;margin-left:4px;;text-align:left">
    <ul id="treeUser" class="ztree" style="margin-top:0; width:90%; height: 480px;"></ul>
     </div>
    </td>
    </tr>
    </table>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
      <ContentTemplate>
         <asp:LinkButton ID="lnkSave" runat="server" onclick="lnkSave_Click"></asp:LinkButton>
         <asp:HiddenField ID="hdResult" runat="server" />
      </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
</html>
